package com.dhu.cst.ningtianzhe181310419.test;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.dhu.cst.ningtianzhe181310419.test.Json.UserInfo;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterPasswordActivity extends AppCompatActivity { ;
    public EditText password,confirmPassword;
    public String pswd;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_password);
        this.getSupportActionBar().hide();
        initView();
    }

    public void initView(){
        password = this.findViewById(R.id.password);
        confirmPassword = this.findViewById(R.id.confirmPassword);
        sharedPreferences = getSharedPreferences("account",MODE_PRIVATE);
    }

    public void finish(View view){
        if(password.getText().toString().equals(confirmPassword.getText().toString())){
            Toast.makeText(RegisterPasswordActivity.this,"恭喜，注册成功！",Toast.LENGTH_SHORT).show();
            pswd = password.getText().toString();
            /*editor = sharedPreferences.edit();
            editor.putString(RegisterActivity.number,pswd);
            editor.commit();*/
            postRegister();
            postInfo();
            Intent intent = new Intent();
            intent.setClass(RegisterPasswordActivity.this,MainActivity.class);
            RegisterPasswordActivity.this.startActivity(intent);
        }
        else{
            Toast.makeText(RegisterPasswordActivity.this,"两次输入不一致，请重新输入",Toast.LENGTH_SHORT).show();
        }
    }

    public void postRegister(){
        OkHttpClient okHttpClient = new OkHttpClient();
        String json = "{\n" +
                "  \"password\": \"" + RegisterActivity.number + "\",\n" +
                "  \"role\": 0,\n" +
                "  \"username\": \"" + pswd + "\"\n" +
                "}";
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        Request request = new Request.Builder()
                .url("http://106.15.200.177:8081/api/users/register")
                .post(requestBody)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RegisterPasswordActivity.this,"注册失败！",Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String res = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RegisterPasswordActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    public void postInfo(){
        OkHttpClient okHttpClient = new OkHttpClient();
        UserInfo userInfo = new UserInfo(Info.address,CommonUtil.getTime(),Info.email,0,Info.name,Info.phoneNumber,"办公",Info.phoneNumber);
        Gson gson = new Gson();
        String json = gson.toJson(userInfo);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        Request request = new Request.Builder()
                .url("http://106.15.200.177:8081/api/contactInfo/insert")
                .post(requestBody)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RegisterPasswordActivity.this,"信息提交失败！",Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String res = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RegisterPasswordActivity.this,"信息提交成功！",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}
